package screen

import (
	"fmt"
	"gexcel/pkg/common"
	"gexcel/pkg/excel"
	"github.com/gdamore/tcell"
	"strconv"
)

var (
	CellWidthDefault             = 10
	StyleSheetDataCellHead       = tcell.StyleDefault.Foreground(tcell.ColorGrey).Background(tcell.ColorWhite)
	StyleSheetDataCellHeadChoice = tcell.StyleDefault.Foreground(tcell.ColorGrey).Background(tcell.ColorPowderBlue)
	StyleSheetDataCellWord       = tcell.StyleDefault.Foreground(tcell.ColorBlack).Background(tcell.ColorWhite)
	StyleSheetDataCellWordOdd    = tcell.StyleDefault.Foreground(tcell.ColorBlack).Background(tcell.ColorLightGrey)
	StyleSheetDataCellWordEven   = tcell.StyleDefault.Foreground(tcell.ColorBlack).Background(tcell.ColorWhite)
	StyleSheetDataCellWordChoice = tcell.StyleDefault.Foreground(tcell.ColorBlack).Background(tcell.ColorOrange)
	StyleSheetDataCellBorder     = tcell.StyleDefault.Foreground(tcell.ColorGrey).Background(tcell.ColorWhite)
)

func (s *Screen) LoadSheetData() {
	s.SheetData, _ = s.ExcelObj.GetSheetData(excel.Filter{Sheet: s.SheetList[s.SheetCur]})
}

func (s *Screen) ShowSheetData() {
	s.SheetCellWidth = []int{} // 重置每个单元格的宽度
	s.ScreenPaintFuncClear(PageSheetData)
	s.ScreenPaintFuncAdd(PageSheetData, PaintFunc{Func: s.DrawSheetDataHead, Params: ParamsDrawSheetData{}})
}

// UpdateSheetCellCur 当界面初始位置变动后，如果当前激活Cell在界面外，则更新到界面内
func (s *Screen) UpdateSheetCellCur() {
	if s.SheetCellCurY < -s.SheetDataStartPointY { // 更新行
		s.SheetCellCurY = -s.SheetDataStartPointY
	} else if s.SheetCellCurY > s.ScreenHeight-s.SheetDataStartPointY {
		s.SheetCellCurY = s.ScreenHeight - s.SheetDataStartPointY - 3 // 要去掉第一行和最后一行，再去1，所以减3
	}
	curPosition := common.Sum(s.SheetCellWidth[:s.SheetCellCurX]) + s.SheetDataLineNumberWidth
	if curPosition < -s.SheetDataStartPointX {
		if i, err := common.GetPosition(s.SheetCellWidth, -s.SheetDataStartPointX); err == nil {
			s.SheetCellCurX = i
		}
	} else if curPosition > -s.SheetDataStartPointX+s.ScreenWidth {
		if i, err := common.GetPosition(s.SheetCellWidth, -s.SheetDataStartPointX+s.ScreenWidth); err == nil {
			s.SheetCellCurX = i - 1
		}
	}
}

func (s *Screen) GetCellWidth(SheetCellX int) int {
	if SheetCellX < 0 || SheetCellX >= len(s.SheetCellWidth) {
		return CellWidthDefault
	}
	return s.SheetCellWidth[SheetCellX]
}

func (s *Screen) GetCellWidthSum(SheetCellX int) int {
	if SheetCellX < 0 {
		return 0
	} else if SheetCellX <= len(s.SheetCellWidth) {
		return common.Sum(s.SheetCellWidth[:SheetCellX])
	}
	return common.Sum(s.SheetCellWidth[:SheetCellX]) //+ CellWidthDefault*(SheetCellX-len(s.SheetCellWidth))
}

// UpdateScreenCur 当激活Cell变动到界面外，更新屏幕到新的位置
func (s *Screen) UpdateScreenCur() {
	if s.SheetCellCurY < -s.SheetDataStartPointY { // 更新行
		s.SheetDataStartPointY = -s.SheetCellCurY
	} else if s.SheetCellCurY > s.ScreenHeight-s.SheetDataStartPointY-3 {
		s.SheetDataStartPointY = s.ScreenHeight - s.SheetCellCurY - 3 // 要去掉第一行和最后一行，再去1，所以减3
	}
	curPosition := s.GetCellWidthSum(s.SheetCellCurX) + s.SheetDataLineNumberWidth
	if curPosition < -s.SheetDataStartPointX {
		s.LogInfo(fmt.Sprintf("<<< [%v]-[%v]", curPosition, s.SheetDataStartPointX))
		s.SheetDataStartPointX = -curPosition
	} else if curPosition > -s.SheetDataStartPointX+s.ScreenWidth-s.GetCellWidth(s.SheetCellCurX) {
		s.LogInfo(fmt.Sprintf(">>> [%v]-[%v]", curPosition, s.SheetDataStartPointX))
		s.SheetDataStartPointX = -curPosition + s.ScreenWidth - s.GetCellWidth(s.SheetCellCurX)
	}
}

type ParamsDrawSheetData struct {
	PointStartX int
	PointStartY int
	PointEndX   int
	PointEndY   int
}

func (s *Screen) DrawSheetDataHead(ParamsIf interface{}) {
	for r, DataRow := range s.SheetData {
		if r > s.ScreenHeight-s.SheetDataStartPointY {
			// 超出页面高度的数据不绘制
			break
		} else if r < -s.SheetDataStartPointY {
			continue
		}
		for c, DataCell := range DataRow {
			if c >= len(s.SheetCellWidth) {
				s.SheetCellWidth = append(s.SheetCellWidth, CellWidthDefault)
			}
			DataCell = s.CutStrByWidth(DataCell, s.SheetCellWidth[c])
			if r == s.SheetCellCurY && c == s.SheetCellCurX {
				s.EmitStr(s.GetCellWidthSum(c)+s.SheetDataLineNumberWidth+s.SheetDataStartPointX, r+1+s.SheetDataStartPointY, StyleSheetDataCellWordChoice, DataCell)
				//s.EmitStr((c+1)*CellWidthDefault, r+1, StyleSheetDataCellWordChoice, DataCell)
			} else {
				if c%2 == 1 {
					s.EmitStr(s.GetCellWidthSum(c)+s.SheetDataLineNumberWidth+s.SheetDataStartPointX, r+1+s.SheetDataStartPointY, StyleSheetDataCellWordOdd, DataCell)
				} else {
					s.EmitStr(s.GetCellWidthSum(c)+s.SheetDataLineNumberWidth+s.SheetDataStartPointX, r+1+s.SheetDataStartPointY, StyleSheetDataCellWord, DataCell)
				}
				//s.EmitStr((c+1)*CellWidthDefault, r+1, StyleSheetDataCellWord, DataCell)
			}
		}

		lineNumStr := s.CutStrByWidth(strconv.Itoa(r+1), s.SheetDataLineNumberWidth)
		if r == s.SheetCellCurY {
			s.EmitStr(0, r+1+s.SheetDataStartPointY, StyleSheetDataCellHeadChoice, lineNumStr) // 显示行数
		} else {
			s.EmitStr(0, r+1+s.SheetDataStartPointY, StyleSheetDataCellHead, lineNumStr) // 显示行数
		}
	}
	for i := 0; i < len(s.SheetCellWidth); i++ { // 显示列标题
		FieldCol := common.ConvertToAlphabet(i + 1)
		FieldCol = s.CutStrByWidth(" "+FieldCol, s.SheetCellWidth[i])
		if i == s.SheetCellCurX {
			s.EmitStr(s.GetCellWidthSum(i)+s.SheetDataLineNumberWidth+s.SheetDataStartPointX, 0, StyleSheetDataCellHeadChoice, FieldCol)
		} else {
			s.EmitStr(s.GetCellWidthSum(i)+s.SheetDataLineNumberWidth+s.SheetDataStartPointX, 0, StyleSheetDataCellHead, FieldCol)
		}
	}
}
